package com.hsamgle.ui;

import com.hsamgle.service.Handler;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @feature: TODO
 * @packge: com.hsamgle.ui
 * @author: 黄鹤老板
 * @create_time: 2019/7/6 13:10
 * @company: 江南皮革厂
 */
public class DomainForm {
    private JLabel localIp;
    private JTextField localIpValue;
    private JTextField publicIpValue;
    private JLabel publicIp;
    private JTextField lastTimeSync;
    private JLabel lastTimeSyncLabel;
    private JTextField domain;
    private JTextField status;
    private JButton syncNow;
    private JPanel panel;
    private JTextField lastTimePublicIp;
    private JTextArea console;
    private JLabel copyright;

    public DomainForm ( ) {
        panel.setAutoscrolls(true);
        localIpValue.setEditable(false);
        publicIpValue.setEditable(false);
        lastTimeSync.setEditable(false);
        status.setEditable(false);
        console.setEditable(false);
        lastTimePublicIp.setEditable(false);
        console.setLineWrap(true);
        console.setWrapStyleWord(true);
        console.setAutoscrolls(true);
        copyright.setFont(new Font("宋体",Font.PLAIN,10));
        copyright.setHorizontalAlignment(JTextField.CENTER);
        panel.setPreferredSize(new Dimension(300,400));
        syncNow.setPreferredSize(new Dimension(100,50));
        syncNow.addActionListener(e -> Handler.syncNow());
        console.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate ( DocumentEvent e ) {
                console.setCaretPosition(console.getText().length());
            }

            @Override
            public void removeUpdate ( DocumentEvent e ) {

            }

            @Override
            public void changedUpdate ( DocumentEvent e ) {

            }
        });

    }

    public JPanel getPanel ( ) {
        return panel;
    }

    public void setLocalIpValue ( String localIpValue ) {
        this.localIpValue.setText(localIpValue);
    }


    public void setPublicIpValue ( String publicIpValue ) {
        this.publicIpValue.setText(publicIpValue);
    }

    public void setLastTimeSync ( String lastTimeSync ) {
        this.lastTimeSync .setText(lastTimeSync);
    }

    public void setDomain ( String domain ) {
        this.domain.setText(domain);
    }

    public void setStatus ( String status ) {
        if(status.contains("成功")){
            this.status.setForeground(Color.GREEN);
        }else if(status.contains("异常")){
            this.status.setForeground(Color.ORANGE);
        }else {
            this.status.setForeground(Color.RED);
        }
        this.status.setText(status);
    }

    public void appendLog ( String log ) {LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String now = formatter.format(ldt);
        String logdata = now + ": " + log + "\n";
        System.out.println(logdata);
        this.console.append(logdata);
    }

    public void setLastTimePublicIp ( String lastTimePublicIp ) {
        this.lastTimePublicIp.setText(lastTimePublicIp);
    }

    public String getDomain(){
        return domain.getText();
    }

    public String getPublicIp ( ) {
        return publicIpValue.getText();
    }

    private void createUIComponents ( ) {
        // TODO: place custom component creation code here
    }
}
